package com.tiennt.android.droidcamreader;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MaskView extends View {
	public enum State {
		DRAWING, ERASING
	};

	State editMode;
	Paint mPaint;
	private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
	Path path;

	public MaskView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		editMode = State.DRAWING;

		mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		mPaint.setARGB(170, 0, 0, 0);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(30);
	}

	public MaskView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		editMode = State.DRAWING;

		mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		mPaint.setARGB(170, 0, 0, 0);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(30);

	}

	public MaskView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		editMode = State.DRAWING;

		mPaint = new Paint();
		mPaint.setDither(true);
		mPaint.setARGB(170, 0, 0, 0);
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(30);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			path = new Path();
			path.moveTo(event.getX(), event.getY());
			// path.lineTo(me.getX(), me.getY());
			pointsToDraw.add(path);
			invalidate();
		} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
			path.lineTo(event.getX(), event.getY());
			invalidate();
		} else if (event.getAction() == MotionEvent.ACTION_UP) {
			// path.lineTo(me.getX(), me.getY());
		}
		return true;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		for (Path path : pointsToDraw) {
			canvas.drawPath(path, mPaint);
		}
	}

}
